package com.kejso.util;

import java.util.*;

/**
 * Created by DELL on 2018/4/23.
 */
public class SortUtil {

    public static Map<String, Double> sort(List<String> fields,List<Double> fieldWeights){
        Map<String,Double> map = new HashMap<>();
        for(int i=0;i<fields.size() && fields.size()==fieldWeights.size();i++){
            map.put(fields.get(i),fieldWeights.get(i));
        }
        map = sortByValue(map);
        return map;
    }

    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map )
    {
        List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>( map.entrySet() );
        Collections.sort( list, new Comparator<Map.Entry<K, V>>()
        {
            public int compare( Map.Entry<K, V> o1, Map.Entry<K, V> o2 )
            {
                return (o2.getValue()).compareTo( o1.getValue() );
            }
        } );

        Set<String> names = new HashSet<>();
        Map<K, V> result = new LinkedHashMap<K, V>();
        for (Map.Entry<K, V> entry : list)
        {
            if(entry.getKey().toString().split(",").length==2){
                if(names.contains(entry.getKey().toString().split(",")[0])){
                    continue;
                }else{
                    names.add(entry.getKey().toString().split(",")[0]);
                }
            }

            result.put( entry.getKey(), entry.getValue() );
        }
        return result;
    }
}
